package zzxkj.blog.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import zzxkj.blog.cacheService.EchartsDataService;
import zzxkj.blog.service.BlogService;
import zzxkj.blog.service.SearchService;
import zzxkj.blog.utils.EsUtil;

@Controller
@RequestMapping("/admin")
public class IndexController {
    @Autowired
    BlogService blogService;
    @Autowired
    SearchService searchService;
    @Autowired
    EchartsDataService dataService;
    @Autowired
    EsUtil esUtil;
    @GetMapping("/index")
    public String index(){
        return "admin/index-charts";
    }
    @GetMapping("/search")
    public String search(){
        return "admin/search-init";
    }

    /**
     * 初始化es
     * @param redirectAttributes
     * @return
     */
    @GetMapping("/searchInit")
    public String searchInit(RedirectAttributes redirectAttributes){
        Long total = esUtil.esInit();
        if(total == null){
            redirectAttributes.addFlashAttribute("message","Es初始化失败，请查看日志!");
        }else {
            String message = "ES索引初始化成功，共 " + total + " 条记录！";
            redirectAttributes.addFlashAttribute("message",message);
        }
        return "redirect:/admin/search";
    }
}
